package kd.xkqj.unittest.plugin.setting;


import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @description: 插件配置UI
 * @author feng_jun_peng
 * @create 2024-12-18 09:17
 * @version 1.0
 */
public class PluginSettingsConfigurable implements Configurable {
    private PluginSettingsUI ui;

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "单元测试类生成器配置"; // 在设置页面中显示的名称
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        ui = new PluginSettingsUI();
        reset(); // 初始化 UI 数据
        return ui.getMainPanel();
    }

    @Override
    public boolean isModified() {
        PluginSettingsService.State state = PluginSettingsService.getInstance().getState();
        return ui.isShowConfirmSelected() != state.showConfirm ||
                ui.isGenPublicSelected() != state.genPublic ||
                ui.isGenProtectedSelected() != state.genProtected ||
                ui.isGenPrivateSelected() != state.genPrivate ||
                ui.isAddUnsupportedExSelected() != state.addUnsupportedEx ||
                ui.isAutoExtendPluginSelected() != state.autoExtendPlugin ||
                ui.isAddDisplayNameSelected() != state.addDisplayName ||
                ui.isAddNOClientMockSelected() != state.addNOClientMock ||
                ui.isAddCustomParamsSelected() != state.addCustomParams ||
                ui.isAutoGenJWYTestSelected() != state.autoGenJWYTest ||
                ui.getImportPackages() != state.importPackages;
    }

    @Override
    public void apply() {
        PluginSettingsService.State state = PluginSettingsService.getInstance().getState();
        state.showConfirm = ui.isShowConfirmSelected();
        state.genPublic = ui.isGenPublicSelected();
        state.genProtected = ui.isGenProtectedSelected();
        state.genPrivate = ui.isGenPrivateSelected();
        state.addUnsupportedEx = ui.isAddUnsupportedExSelected();
        state.autoExtendPlugin = ui.isAutoExtendPluginSelected();
        state.addDisplayName = ui.isAddDisplayNameSelected();
        state.addNOClientMock = ui.isAddNOClientMockSelected();
        state.addCustomParams = ui.isAddCustomParamsSelected();
        state.autoGenJWYTest = ui.isAutoGenJWYTestSelected();
        state.importPackages = ui.getImportPackages();
    }

    @Override
    public void reset() {
        PluginSettingsService.State state = PluginSettingsService.getInstance().getState();
        ui.setShowConfirmSelected(state.showConfirm);
        ui.setGenPublicSelected(state.genPublic);
        ui.setGenProtectedSelected(state.genProtected);
        ui.setGenPrivateSelected(state.genPrivate);
        ui.setAddUnsupportedExSelected(state.addUnsupportedEx);
        ui.setAutoExtendPluginSelected(state.autoExtendPlugin);
        ui.setAddDisplayNameSelected(state.addDisplayName);
        ui.setAddNOClientMockSelected(state.addNOClientMock);
        ui.setAddCustomParamsSelected(state.addCustomParams);
        ui.setAutoGenJWYTestSelected(state.autoGenJWYTest);
        ui.setImportPackages(state.importPackages);

    }

    @Override
    public void disposeUIResources() {
        ui = null; // 释放资源
    }
}

